package org.tinygroup.trans.xstream.util;

import org.tinygroup.trans.xstream.manager.XStreamConvertService;
import org.tinygroup.trans.xstream.manager.XStreamTransFactory;

import com.thoughtworks.xstream.XStream;

public class XStreamConvertUtil {
	static XStreamTransFactory factory;

	public static void setXStreamConvertService(
			XStreamConvertService xStreamConvertService) {
		if (factory == null) {
			factory = new XStreamTransFactory();
		}
		factory.setxStreamConvertService(xStreamConvertService);
	}

	public static Object convert(Object obj, String scene) {
		XStream xStream = factory.getXStreamByScene(scene);
		if (obj instanceof String) {
			return xml2Object(xStream, (String) obj);
		} else {
			return object2Xml(xStream, obj);
		}
	}

	public static Object xml2Object(XStream xStream, String xml) {
		return xStream.fromXML(xml);
	}

	public static String object2Xml(XStream xStream, Object obj) {
		return xStream.toXML(obj);
	}

}
